package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntContactMapper;
import com.prd.model.EntBudget;
import com.prd.model.EntContact;
import com.prd.model.EntContactReply;
import com.prd.model.LoginUser;
import com.prd.service.EntContactReplyService;
import com.prd.service.EntContactService;
import com.prd.service.UserService;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import com.prd.vo.EntContactVo;
import com.prd.vo.UserBaseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntContactServiceImpl extends ServiceImpl<EntContactMapper, EntContact> implements EntContactService {

    @Autowired
    private EntContactMapper entContactMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private EntContactReplyService entContactReplyService;

    @Override
    public int insertInfo(EntContact entContact) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entContact);
        entContact.setNumber(String.valueOf(System.nanoTime()));
        entContact.setIsRecall(IsStatus.TWO.getCode());
        return entContactMapper.insert(entContact);
    }

    @Override
    public int updateInfo(EntContact entContact) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entContact);
        return entContactMapper.updateById(entContact);
    }

    @Override
    public List<EntContact> listInfo(String day, Integer catalog) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntContact> queryWrapper = new LambdaQueryWrapper<>();
        //全部
        if (catalog == 0) {

        } else if (catalog == 1) {//我发起
            queryWrapper.eq(EntContact::getInitiator, loginUser.getId());
        } else if (catalog == 2) {//我接收
            queryWrapper.eq(EntContact::getMatters, loginUser.getId());
            queryWrapper.or().apply("find_in_set({0},make)", loginUser.getId());
        } else if (catalog == 3) {//我制单
            queryWrapper.eq(EntContact::getCreateId, loginUser.getId());
        } else if (catalog == 4) {//待确认
            queryWrapper.isNull(EntContact::getId);
        } else if (catalog == 5) {//已确认
            queryWrapper.isNull(EntContact::getId);
        } else if (catalog == 6) {//待审批
            queryWrapper.isNull(EntContact::getId);
        } else if (catalog == 7) {//审批通过
            queryWrapper.isNull(EntContact::getId);
        } else if (catalog == 8) {//审批不通过
            queryWrapper.isNull(EntContact::getId);
        } else {
            throw new RuntimeException("找不到类型！");
        }
        List<EntContact> list = entContactMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public EntContactVo details(Long id) {
        EntContactVo entContactVo = new EntContactVo();
        EntContact entContact = entContactMapper.selectById(id);
        if (entContact == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entContact, entContactVo);
        LambdaQueryWrapper<EntContactReply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntContactReply::getContactId, id);
        entContactVo.setContactReply(entContactReplyService.list(queryWrapper));

        //获取用户信息
        List<Long> arrayList = new ArrayList<>();
        List<Long> makes = Arrays.asList(StringsUtils.convert(entContact.getMake().split(",")));
        arrayList.addAll(makes);
        arrayList.add(StringsUtils.isNotBlank(entContact.getInitiator()) ? 0 : Long.valueOf(entContact.getInitiator()));
        arrayList.add(StringsUtils.isNotBlank(entContact.getMatters()) ? 0 : Long.valueOf(entContact.getMatters()));
        arrayList.stream().distinct().collect(Collectors.toList());
        List<UserBaseVo> listUser = userService.listName(arrayList.toArray(new Long[arrayList.size()]));
        List<String> listMake = new ArrayList<>();
        for (UserBaseVo userBaseVo : listUser) {
            if (entContact.getInitiator().equals(userBaseVo.getId().toString())) {
                entContactVo.setInitiator(userBaseVo.getMerge());
            }
            if (entContact.getMatters().equals(userBaseVo.getId().toString())) {
                entContactVo.setMatters(userBaseVo.getMerge());
            }
            if (makes.contains(userBaseVo.getId())) {
                listMake.add(userBaseVo.getMerge());
            }
        }
        entContactVo.setMakes(listMake);
        return entContactVo;
    }

    @Override
    public int addMake(Long id, Long[] makes) {
        EntContact entContact = entContactMapper.selectById(id);
        if (entContact == null) {
            throw new RuntimeException("记录没有找到！");
        }
        if (StringUtils.isNotBlank(entContact.getMake())) {
            entContact.setMake(entContact.getMake() + "," + makes.toString());
        } else {
            entContact.setMake(makes.toString());
        }
        return updateInfo(entContact);
    }

    @Override
    public int recall(Long id) {
        EntContact entContact = entContactMapper.selectById(id);
        if (entContact == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entContact.setIsRecall(IsStatus.ONE.getCode());
        return updateInfo(entContact);
    }

}
